package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.service.SellerAddressService;
import cn.signalpha.modules.shop.vo.SellerDeliveryConfigVO;
import cn.signalpha.modules.shop.vo.SellerExtVO;
import cn.signalpha.modules.shop.vo.SellerPickConfigVO;
import cn.signalpha.modules.shop.vo.SellerVO;

import java.util.List;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author signalpha
 */
public class SellerWrapper extends BaseEntityWrapper<Seller, SellerVO> {

	private static final SellerAddressService sellerAddressService;

	static {
		sellerAddressService = SpringUtil.getBean(SellerAddressService.class);
	}

	public static SellerWrapper build() {
		return new SellerWrapper();
	}

	@Override
	public SellerVO entityVO(Seller seller) {
		SellerVO sellerVO = Objects.requireNonNull(BeanUtil.copy(seller, SellerVO.class), MsgConstant.OBJECT_NULL);
		sellerVO.setExtVO(JsonUtil.parse(sellerVO.getExt(), SellerExtVO.class));
		if(sellerVO.getIsOpenPick() == 1){
			List<SellerPickConfigVO> sellerPickConfigList = JsonUtil.parseArray(sellerVO.getPickConfig(), SellerPickConfigVO.class);
			sellerPickConfigList.forEach(pickConfigVO -> {
				pickConfigVO.setAddress(sellerAddressService.getById(pickConfigVO.getAddressId()));
			});
			sellerVO.setSellerPickConfigList(sellerPickConfigList);
		}
		if(sellerVO.getIsOpenDelivery() == 1){
			sellerVO.setSellerDeliveryConfig(JsonUtil.parse(sellerVO.getDeliveryConfig(), SellerDeliveryConfigVO.class));
		}
		return sellerVO;
	}

	public SellerVO entityVO(Seller seller, Long userId) {
		return entityVO(seller);
	}

}
